package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.MemberInfo;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service(interfaceClass = MenuService.class)
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    @Override
    public PageResult finpage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        PageHelper.startPage(currentPage, pageSize);
        Page<Menu> page = menuDao.finpage(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }


    @Override
    public void Update(Menu menu) {
        menuDao.Update(menu);
    }


    @Override
    public void deleteById(Integer id) {
        //判断当前检查项是否已经关联到检查组
        long count = menuDao.findCountByMenuId(id);
        if(count > 0){
            //当前检查项已经被关联到检查组，不允许删除
            new RuntimeException("删除失败");
        }
        menuDao.deleteById(id);
    }

    @Override
    public List<Integer> findLala() {
        return menuDao.findLala();
    }


    @Override
    public Menu findByName() {
        return menuDao.findByName();
    }

   /* @Override
    public List<MemberInfo> findByIds(Integer[] ids) {
        return null;
    }*/
   @Override
   public List<MemberInfo> findByIds(Integer[] ids) {
       List<MemberInfo> list = new ArrayList<>();
       for (Integer id : ids) {
           List<MemberInfo> memberinfos = menuDao.findByIds(id);
           for (MemberInfo memberinfo : memberinfos) {
               String sex = memberinfo.getSex();
               if(sex != null) {
                   if ("1".equals(sex)){
                       memberinfo.setSex("男");
                   } else {
                       memberinfo.setSex("女");
                   }
               } else {
                   memberinfo.setSex("未知");
               }
               list.add(memberinfo);
           }
       }
       return list;
   }

}
